import java.util.Arrays;

public class Test {

    public static void main5(String[] args) {
        int[] array = {1,2,3,4,5};
        //Java当中一般给定范围时是[1,3)
        int[] copy = Arrays.copyOfRange(array,1,3);
        System.out.println(Arrays.toString(copy));

    }


    public static void main4(String[] args) {
        int[] array = {1,2,3,4,5};
        //扩容操作
        int[] copy = Arrays.copyOf(array,array.length*2);
        array = copy; //array所指向的引用指向了copy所指向的引用
        for (int x:copy) {
            System.out.print(x+" ");
        }
        System.out.println();

        System.out.println(Arrays.toString(array));
    }

    public static void main3(String[] args) {
        int[] array = {1,2,3,4,5};
        int[] copy = new int[array.length];
        //Arrays.copyOf使用的底层代码逻辑（打开copyOf源代码可查看）
        System.arraycopy(array,0,copy,0,array.length);
        //点开arraycopy源代码，有个native修饰，是C/C++代码逻辑，native在本地方法栈开辟内存空间

        for (int x:copy) {
            System.out.print(x+" ");
        }
    }



    //使用Arrays.copyOf方法进行拷贝，输入原数组名，原数组长度
    public static void main2(String[] args) {
        int[] array = {1,2,3,4,5};
        int[] copy = Arrays.copyOf(array,array.length);

        for (int x:array) {
            System.out.print(x+" ");
        }
    }

    //数组拷贝，自己实现,创建一个一样大小的新数组，然后一一复制
    public static void main1(String[] args) {
        int[] array = {1,2,3,4,5};

       //int[] copy = null;  错误定义，copy数组指向空引用,运算不出来
        //正确
        int[] copy = new int[array.length];

        for (int i = 0; i < array.length; i++) {
            copy[i] =array[i];
        }
        for (int x:copy) {
            System.out.print(x + " ");
        }
    }
}
